package priv.yiyuan.example.design.flyweight;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂类
 *
 * @author YiYuan
 * @data 2025/9/19
 * @apoNote
 */
public class StrategyFactory {

    private static final Map<String, PriceCalculationStrategy> strategies = new HashMap<>();

    public static PriceCalculationStrategy getStrategy(String type, double... params) {

        String key = type + "-" + Arrays.toString(params);

        PriceCalculationStrategy strategy = strategies.get(key);

        if (strategy == null) {
            switch (type) {
                case "Discount":
                    strategy = new DiscountStrategy(params[0]);
                    break;
                case "FullReduction":
                    strategy = new FullReductionStrategy(params[0], params[1]);
                    break;
                default:
                    throw new IllegalArgumentException("未知策略类型: " + type);
            }

            strategies.put(key, strategy);
        }

        return strategy;

    }

}
